package com.dididoctor.patient.Activity.Usercentre.Record.Health;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.dididoctor.patient.MV.BaseFragment;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Utils.Util;

/**
 * Created by Administrator on 2016/4/14.
 */
public class HealthFragment extends BaseFragment implements HealthView {

    private Activity activity;
    private EditText mEtJob, mEtAgfood, mEtAgpill;
    private TextView mTvName, mTvSex, mTvAge;
    private HealthPresenter presenter;
    private HealthBean healthBean = new HealthBean();

    private String job = "", agfood = "", agpill = "";
    private SubmitReceiver receiver;

    @Override
    public void onAttach(Activity activity) {
        this.activity = activity;
        super.onAttach(activity);
    }

    public HealthFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_healthy, null);
        this.initView(rootView);
        initmyBroadcast();
        return rootView;

    }

    private void initView(View rootView) {
        mEtJob = (EditText) rootView.findViewById(R.id.et_job);
        mEtAgfood = (EditText) rootView.findViewById(R.id.et_agfood);
        mEtAgpill = (EditText) rootView.findViewById(R.id.et_agpill);
        mTvName = (TextView) rootView.findViewById(R.id.tv_name);
        mTvAge = (TextView) rootView.findViewById(R.id.tv_age);
        mTvSex = (TextView) rootView.findViewById(R.id.tv_sex);
        presenter = new HealthPresenter(activity, this);
        presenter.gethealth();

    }

    public void updatehealth() {
        job = mEtJob.getText().toString();
        agfood = mEtAgfood.getText().toString();
        agpill = mEtAgpill.getText().toString();
        if (Util.isEmpty(job) || Util.isEmpty(agfood) || Util.isEmpty(agpill)) {
            showToastMessage("请将信息填写完整");
        } else {
            presenter.updatehealth(job, agfood, agpill);
        }

    }

    @Override
    public void gethealthsucced(HealthBean healthBean) {
        if (healthBean != null) {
            this.healthBean = healthBean;
            mTvName.setText(healthBean.getName());
            mTvAge.setText(healthBean.getAge());
            mTvSex.setText("1".equals(healthBean.getSex()) ? "男" : "女");
            mEtJob.setText(healthBean.getJob());
            mEtAgfood.setText(healthBean.getAgfood());
            mEtAgpill.setText(healthBean.getAgpill());
        }
    }

    @Override
    public void gethealthfail() {
        showToastMessage("加载失败");
    }

    @Override
    public void updatesucced() {
        showToastMessage("保存成功");
    }

    @Override
    public void updatefail() {
        showToastMessage("修改失败");
    }


    private void initmyBroadcast() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("SubmitReceiver");
        receiver = new SubmitReceiver();
        activity.registerReceiver(receiver, intentFilter);
    }

    public class SubmitReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            updatehealth();
        }
    }

    @Override
    public void onDestroy() {
        getActivity().unregisterReceiver(receiver);
        super.onDestroy();
    }
}
